﻿<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="wfrmValidaCNS.aspx.vb" Inherits="ProjetoDeTestes.wfrmValidaCNS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Valida CNS</title>
    <script type="text/javascript">
        function validaCNS(vlrCNS) {
            // Formulário que contem o campo CNS
            var soma = new Number;
            var resto = new Number;
            var dv = new Number;
            var pis = new String;
            var resultado = new String;
            var tamCNS = vlrCNS.length;
            if ((tamCNS) != 15) {
                alert("Numero de CNS invalido");
                return false;
            }
            pis = vlrCNS.substring(0, 11);
            soma = (((Number(pis.substring(0, 1))) * 15) +
	        ((Number(pis.substring(1, 2))) * 14) +
		    ((Number(pis.substring(2, 3))) * 13) +
		    ((Number(pis.substring(3, 4))) * 12) +
            ((Number(pis.substring(4, 5))) * 11) +
            ((Number(pis.substring(5, 6))) * 10) +
            ((Number(pis.substring(6, 7))) * 9) +
            ((Number(pis.substring(7, 8))) * 8) +
            ((Number(pis.substring(8, 9))) * 7) +
            ((Number(pis.substring(9, 10))) * 6) +
            ((Number(pis.substring(10, 11))) * 5));
            resto = soma % 11;
            dv = 11 - resto;
            if (dv == 11) {
                dv = 0;
            }
            if (dv == 10) {
                soma = (((Number(pis.substring(0, 1))) * 15) +
	            ((Number(pis.substring(1, 2))) * 14) +
		    	((Number(pis.substring(2, 3))) * 13) +
		    	((Number(pis.substring(3, 4))) * 12) +
            	((Number(pis.substring(4, 5))) * 11) +
            	((Number(pis.substring(5, 6))) * 10) +
            	((Number(pis.substring(6, 7))) * 9) +
            	((Number(pis.substring(7, 8))) * 8) +
            	((Number(pis.substring(8, 9))) * 7) +
            	((Number(pis.substring(9, 10))) * 6) +
            	((Number(pis.substring(10, 11))) * 5) + 2);
                resto = soma % 11;
                dv = 11 - resto;
                resultado = pis + "001" + String(dv);
            } else {
                resultado = pis + "000" + String(dv);
            }
            if (vlrCNS != resultado) {
                alert("Numero de CNS invalido");
                return false;
            } else {
                alert("Numero de CNS válido");
                return true;
            }
        }
    </script>
    <style type="text/css">
        #Text1
        {
            width: 191px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table cellpadding="0" cellspacing="0" style="width: 100%">
            <tr >
                <td align="right" style="width: 250px">
                    <asp:Label ID="Label1" runat="server" Text="Digite o número do CNS"></asp:Label>
                </td>
                <td>
                    </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Columns="30"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    </td>
                <td>
                    </td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Verificar" />
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    </td>
                <td>
                    </td>
                <td>
                    <asp:Label ID="lblOK" runat="server" Visible="False"></asp:Label>
                    </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>
